var express = require('express');
var router = express.Router();
var moment = require('moment');

var db = require('./../../utils/db.js');

/**
 * 个人任务 控制器
 */
router.get('/index', function (req, res, next) {
    var loginedUser = req.session.loginedUser;
    if (!loginedUser) {
        res.redirect('/login');
    } else {
        res.render('personalTask/index');
    }
});

/**
 * 今日任务
 */
router.get('/todayList', function (req, res, next) {
    var loginedUser = req.session.loginedUser;
    var createDate = moment(new Date()).format('YYYY-MM-DD');
    var todaySql = 'select * from personal_task where is_complete=0 and create_date="' + createDate + '" and assignee_id=' + loginedUser.id;
    db.query(todaySql, function (err, rows) {
        if (err) {
            res.json({
                message: err.message,
                errcode: 1
            });
        } else {
            res.json({
                message: '今日任务获取成功',
                errcode: 0,
                data: rows
            });
        }
    });
});

/**
 * 历史任务
 */
router.get('/historyList', function (req, res, next) {

    var loginedUser = req.session.loginedUser;
    var createDate = moment(new Date()).format('YYYY-MM-DD');
    console.log(createDate)
    var historySql = "select * from personal_task where create_time < '"+ createDate +"' and is_complete=0   and assignee_id=" + loginedUser.id;
    console.log(historySql)
    db.query(historySql, function (err, rows) {
        if (err) {
            res.json({
                message: err.message,
                errcode: 1
            });
        } else {
            res.json({
                message: '历史任务获取成功',
                errcode: 0,
                data: rows
            });
        }
    });
});

/**
 * 保存
 */
router.post('/save', function (req, res, next) {
    var taskName = req.body.taskName;
    var taskNameArr = taskName.split("@");//split方法可以提取字符串   用@符号作为提取的符号
    var assigneeName = taskNameArr[2];
    var assigneesql = "select * from base_user where loginname = '" + assigneeName + "'";
    db.query(assigneesql, function (err, rows) {
        if (err) {
            res.json({
                errcode: 1,
                message: '通过loginname获取用户id失败',
            });
        } else {
            var assigneeId = rows[0].id;
            var level = taskNameArr[1];
            taskName = taskNameArr[0];  //重新声明taskName是输入框的的第一个字符串
            var momentDate = moment(new Date());
            var createTime = momentDate.format('YYYY-MM-DD HH:mm:ss');
            var createDate = momentDate.format('YYYY-MM-DD');
            var createById = req.session.loginedUser ? req.session.loginedUser.id : null;
            var sql = 'insert into personal_task(task_name,create_time,create_date,create_by_id,assignee_id,level,is_complete,remark) values' +
                '("' + taskName + '","' + createTime + '","' + createDate + '",' + createById + ',' + assigneeId + ',' + level + ', 0,"")';
            var meSucceed=false;//为了解决，不是自己的任务出现在自己的任务列表中   声明一个变量当做不正确
            if (assigneeId == createById){   //如果创建人id等于当前用户id
                meSucceed=true;            //上面声明的变量就等于真的
            }
            db.query(sql, function (err, rows) {
                if (err) {
                    res.json({
                        errcode: 1,
                        message: '新增失败',
                        alert:"当前用户名不存在"   //提示错误信息
                });
                } else {
                    res.json({
                        errcode: 0,
                        message: '新增成功',
                        data: {
                            task_name: taskName,
                            create_time: createTime,
                            me_succeed:meSucceed,
                            insertId:rows.insertId
                        }
                    });
                }
            });
        }
    });
});

/**
 * 完成任务
 * is_complate字段置为1
 */
router.post('/complete/:id', function (req, res, next) {
    var id = req.params.id;
    // var completeTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
    var completeTime = new Date();
    var sql = 'update personal_task set is_complete=1, complete_time="' + completeTime + '" where id=' + id;
    db.query(sql, function (err, rows) {
        if (err) {
            res.json({
                errcode: 1,
                message: '更新失败'
            });
        } else {
            res.json({
                errcode: 0,
                message: '更新成功'
            });

        }
    });
});

module.exports = router;
